package com.polaris.live.popup

import android.content.Context
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.common.R
import com.polaris.live.common.databinding.PopupDataSelectBinding
import com.polaris.live.common.ext.convertArabicDigitsToNormal
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.DateUtils
import java.util.Calendar

/**
 * 时间选择弹窗
 *
 * @author Created by 半仙 on 2023/9/4/004 14:17
 */
class DataSelectPopup(
    context: Context,
    private val mOnSelectCallBack: ((year: String, month: String, day: String) -> Unit),
    private val mBirthday: Long? = null,
) : BottomPopupView(context) {

    private lateinit var mBinding: PopupDataSelectBinding
    private var mYear = ""
    private var mMonth = ""
    private var mDay = ""

    override fun getImplLayoutId(): Int {
        return R.layout.popup_data_select
    }

    override fun onCreate() {
        super.onCreate()

        mBinding = PopupDataSelectBinding.bind(popupImplView)

        mBinding.dataSelect.setSelectedTextBold(true)

        val calendar = Calendar.getInstance()
        val currentYear = calendar[Calendar.YEAR] - 18
        val currentMonth = calendar[Calendar.MONTH] + 1
        val currentDay = calendar[Calendar.DAY_OF_MONTH]

        val defaultValue = if (mBirthday == null) {
            DateEntity.target(currentYear, currentMonth, currentDay)
        } else {
            val dateString = DateUtils.convertTimestampToString(mBirthday)
            val time = dateString?.convertArabicDigitsToNormal()?.split("-")
            if (time != null) {
                DateEntity.target(time[0].toInt(), time[1].toInt(), time[2].toInt())
            } else {
                DateEntity.target(currentYear, currentMonth, currentDay)
            }
        }

        val startValue = DateEntity.target(currentYear - 100, 1, 1)
        val endValue = DateEntity.target(currentYear, currentMonth, currentDay)

        mYear = currentYear.toString()
        mMonth = if (currentMonth < 10) "0${currentMonth}" else currentMonth.toString()
        mDay = if (currentDay < 10) "0${currentDay}" else currentDay.toString()

        mBinding.dataSelect.setRange(startValue, endValue, defaultValue)
        mBinding.dataSelect.setDateMode(DateMode.YEAR_MONTH_DAY)
        mBinding.dataSelect.setIndicatorSize(0f)


        mBinding.dataSelect.setOnDateSelectedListener { year, month, day ->
            this.mYear = year.toString()
            this.mMonth = if (month < 10) "0${month}" else month.toString()
            this.mDay = if (day < 10) "0${day}" else day.toString()
        }
        mBinding.icBack.setOnSingleClickListener {
            dismiss()
        }

        mBinding.selectData.setOnSingleClickListener {
            mOnSelectCallBack.invoke(mYear, mMonth, mDay)
            dismiss()
        }
    }


}